package bzapps.weynak;

import java.util.ArrayList;
import java.util.concurrent.Executors;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.location.Location;
import android.view.Window;
import android.widget.TextView;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class FriendOverlay extends ItemizedOverlay<OverlayItem>
{
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context mContext;
	String link, date, distance;
	Location loc;

 public FriendOverlay(Context context, String link, String date, Location l)
 {
	 super(null);
	 mContext = context;
	 this.link = link;
	 this.date = date;
	 this.loc = l;
 }

 @Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {

	super.draw(canvas, mapView, false);
}
 
 public void addOverlay(OverlayItem overlay)
 {
	 mOverlays.add(overlay);
	 populate();
 }
 @Override
 protected OverlayItem createItem(int i)
 {	
	final OverlayItem result = mOverlays.get(i);
	final LayerDrawable marker = (LayerDrawable) mContext.getResources().getDrawable(R.drawable.marker);
    final Drawable photo = WeynakUtils.getToCache(mContext, result.getTitle());

    if (photo != null) {
        marker.setDrawableByLayerId(R.id.photo, photo);
        marker.setDrawableByLayerId(R.id.frame, mContext.getResources().getDrawable(R.drawable.frame));
    } else {
        Drawable frame = new ColorDrawable();
        frame.setBounds(0, 0, 0, 0);
        marker.setDrawableByLayerId(R.id.frame, frame);
        Executors.defaultThreadFactory().newThread(new Runnable() {

            @Override
            public void run()
            {
                final Bitmap bitmap = WeynakUtils.getImageBitmap(link);
                if (bitmap != null) {
                    boolean success = WeynakUtils.storeToCache(mContext, result.getTitle(), bitmap);
                    if (success) {
                        final Drawable photo = WeynakUtils.getToCache(mContext, result.getTitle());
                        marker.setDrawableByLayerId(R.id.photo, photo);
                        marker.setDrawableByLayerId(R.id.frame, mContext.getResources().getDrawable(R.drawable.frame));
                        result.setMarker(boundCenter(marker));
                    }
                }
            }

        }).start();
    }

    result.setMarker(boundCenter(marker));
	 
	
	 return result;	 
 }
 @Override
 public int size()
 {
	 return mOverlays.size();
 }
 @Override
 protected boolean onTap(int index)
 {
	 String unit = "Km"; 
	 Location myloc = Maps.getMyLocation();
	 double dis = WeynakUtils.distanceKm(myloc.getLatitude(), myloc.getLongitude(), loc.getLatitude(), loc.getLongitude());
	    if(dis<1){
	    	dis = dis*1000;
	    	unit= "m";
	    }
	 Dialog mydialog = new Dialog(mContext);
	 mydialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
	 mydialog.setContentView(R.layout.friend_layout);
	 OverlayItem item = mOverlays.get(index);
	 TextView title = (TextView)mydialog.findViewById(R.id.friend_layout_name);
	 TextView loc = (TextView)mydialog.findViewById(R.id.friend_layout_loc);
	 TextView last_date = (TextView)mydialog.findViewById(R.id.friend_layout_date);
	 TextView distanceto = (TextView)mydialog.findViewById(R.id.friend_layout_distance);
	 title.setText(item.getTitle());
	 loc.setText(item.getSnippet()); 
	 last_date.setText(date);
	 distanceto.setText(String.valueOf(dis).substring(0,7) + " " +unit);
	 mydialog.show();

	return true;
 }
}